using System;
using System.Collections;
using System.DirectoryServices;

namespace BEGDC.CC.Entity
{
	#region Email

	/// <summary>
	/// Email object for NHibernate mapped table 'Email'.
	/// </summary>
	[Serializable]
	public class Email : System.IComparable
		{
		#region Member Variables
		
		protected int _id;
		protected int _apID;
		protected string _templateName;
		protected bool _isSend;
		protected Nullables.NullableDateTime _sendTime;
		protected Application _application;
		protected static String _sortExpression = "Id";
		protected static SortDirection _sortDirection = SortDirection.Ascending;

		#endregion

		#region Constructors

		public Email() { }

		public Email( int apID,string templateName, bool isSend, Nullables.NullableDateTime sendTime, Application application)
		{
			this._apID=apID;
			this._templateName = templateName;
			this._isSend = isSend;
			this._sendTime = sendTime;
			this._application = application;
		}

		#endregion

		#region Public Properties

		public int Id
		{
			get {return _id;}
			set {_id = value;}
		}

		public int ApID
		{
			get {return _apID;}
			set {_apID = value;}
		}
		public string TemplateName
		{
			get { return _templateName; }
			set
			{
				if ( value != null && value.Length > 50)
					throw new ArgumentOutOfRangeException("Invalid value for TemplateName", value, value.ToString());
				_templateName = value;
			}
		}

		public bool IsSend
		{
			get { return _isSend; }
			set { _isSend = value; }
		}

		public Nullables.NullableDateTime SendTime
		{
			get { return _sendTime; }
			set { _sendTime = value; }
		}

		public Application Application
		{
			get { return _application; }
			set { _application = value; }
		}

        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }
		#endregion
		
        #region IComparable Methods
        public int CompareTo(object obj)
        {
			if (!(obj is Email))
				throw new InvalidCastException("This object is not of type Email");
			
			int relativeValue;
			switch (SortExpression)
			{
				case "Id":
					relativeValue = this.Id.CompareTo(((Email)obj).Id);
					break;
				case "ApID":
					relativeValue = this.ApID.CompareTo(((Email)obj).ApID);
					break;
				case "TemplateName":
						relativeValue = ( this.TemplateName != null ) ? this.TemplateName.CompareTo(((Email)obj).TemplateName) : -1;
					break;
				case "IsSend":
						relativeValue = ( this.IsSend != Convert.ToBoolean( null) ) ? this.IsSend.CompareTo(((Email)obj).IsSend) : -1;				
					break;
				case "SendTime":
						relativeValue = ( this.SendTime != null ) ? this.SendTime.CompareTo(((Email)obj).SendTime) : -1;
					break;
                default:
                    goto case "Id";
			}
            if (Email.SortDirection == SortDirection.Ascending)
				relativeValue *= -1;
			return relativeValue;
		}
		#endregion
	}

	#endregion
}
